package jp.co.apl.haven_enquete.android.provider;

import jp.co.apl.haven.generated.HavenProtos.InputType;
import jp.co.apl.haven.generated.HavenProtos.Question;
import android.database.Cursor;
import android.net.Uri;
import android.test.ProviderTestCase2;

import com.google.protobuf.InvalidProtocolBufferException;

public class QuestionDataProviderTest extends
        ProviderTestCase2<QuestionDataProvider> {

    public QuestionDataProviderTest() {
        super(QuestionDataProvider.class, "jp.co.apl.haven_enquete");
    }

    protected void setUp() throws Exception {
        super.setUp();
    }

    public void testQuery() throws InvalidProtocolBufferException {
        Uri uri = Uri.parse("content://jp.co.apl.haven_enquete/question/10001");
        Cursor c = getProvider().query(uri, null, null, null, null);

        assertTrue(c.moveToFirst());

        Question q = Question.parseFrom(c.getBlob(0));

        assertEquals(10001, q.getId());
        assertEquals("ゴミの集積（回収頻度）", q.getTitle());
        assertEquals("ゴミの回収頻度は次のうちどの程度ですか？", q.getText());
        assertEquals(4, q.getSelectionItemCount());
    }

    public void testQuery2() throws InvalidProtocolBufferException {
        Uri uri = Uri.parse("content://jp.co.apl.haven_enquete/question/10");
        Cursor c = getProvider().query(uri, null, null, null, null);

        assertTrue(c.moveToFirst());

        Question q = Question.parseFrom(c.getBlob(0));

        assertEquals(10, q.getId());
        assertEquals(1, q.getSelectionItem(0).getChildQuestionCount());
        assertEquals(10001, q.getSelectionItem(0).getChildQuestion(0).getId());
        assertNotNull(q.getSelectionItem(0).getChildQuestion(0));
    }

    public void testQueryFirst() throws InvalidProtocolBufferException {
        Uri uri = Uri.parse("content://jp.co.apl.haven_enquete/question");
        Cursor c = getProvider().query(uri, null, null, null, null);

        assertTrue(c.moveToFirst());

        Question q = Question.parseFrom(c.getBlob(0));

        assertEquals(1, q.getId());
        assertEquals("施設の種類", q.getTitle());
        assertEquals(InputType.SELECT, q.getInputType());
        assertEquals(5, q.getSelectionItemCount());
    }

    public void testQueryNoHit() throws InvalidProtocolBufferException {
        Uri uri = Uri.parse("content://jp.co.apl.haven_enquete/question/97564");
        Cursor c = getProvider().query(uri, null, null, null, null);

        assertFalse(c.moveToFirst());
        assertEquals(0, c.getCount());
    }

}
